package com.dayouzi.crawler_monitor.controller;

import com.dayouzi.crawler_monitor.base.result.R;
import com.dayouzi.crawler_monitor.pojo.page.PageBean;
import com.dayouzi.crawler_monitor.pojo.query.PersonConfigListQuery;
import com.dayouzi.crawler_monitor.pojo.query.PersonConfigQuery;
import com.dayouzi.crawler_monitor.pojo.query.PersonListQuery;
import com.dayouzi.crawler_monitor.pojo.query.UserAdminQuery;
import com.dayouzi.crawler_monitor.pojo.vo.PersonConfigListVO;
import com.dayouzi.crawler_monitor.pojo.vo.PersonListVO;
import com.dayouzi.crawler_monitor.service.PersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

@Api(description = "人员爬虫")
@RestController
@RequestMapping("/crawler/person")
@Slf4j
public class PersonController {

    @Autowired
    private PersonService personService;

    /**
     * 分页查询爬虫信息list
     * @param page
     * @param limit
     * @param personListQuery
     * @return
     */
    @ApiOperation("分页查询人员爬虫list")
    @PostMapping("/listPage/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true) @PathVariable Integer page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Integer limit,
            @ApiParam(value = "查询对象", required = true) @RequestBody PersonListQuery personListQuery
    ) {
        PageBean<PersonListVO> result = personService.listPage(page, limit, personListQuery);
        return R.ok().data("result", result);
    }

    @ApiOperation("获取负责人列表")
    @GetMapping("/getManagerList")
    public R getManagerList() {
        ArrayList<HashMap<String, String>> result = personService.getManagerList();
        return R.ok().data("result", result);
    }

    @ApiOperation("获取队列状态list")
    @GetMapping("/getQueueStatusList")
    public R getQueueStatusList() {
        ArrayList<HashMap<String, String>> result = personService.getQueueStatusList();
        return R.ok().data("result", result);
    }

    @ApiOperation("获取运行状态list")
    @GetMapping("/getRunStatusList")
    public R getRunStatusList() {
        ArrayList<HashMap<String, String>> result = personService.getRunStatusList();
        return R.ok().data("result", result);
    }

    @ApiOperation("获取日志状态list")
    @GetMapping("/getLogStatusList")
    public R getLogStatusList() {
        ArrayList<HashMap<String, String>> result = personService.getLogStatusList();
        return R.ok().data("result", result);
    }

    /**
     * 分页查询信息list
     * @param page
     * @param limit
     * @param personConfigListQuery
     * @return
     */
    @ApiOperation("分页查询人员爬虫配置list")
    @PostMapping("/configListPage/{page}/{limit}")
    public R configListPage(
            @ApiParam(value = "当前页码", required = true) @PathVariable Integer page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Integer limit,
            @ApiParam(value = "查询对象", required = true) @RequestBody PersonConfigListQuery personConfigListQuery
    ) {
        PageBean<PersonConfigListVO> result = personService.configListPage(page, limit, personConfigListQuery);
        return R.ok().data("result", result);
    }

    @ApiOperation("获取负责人列表")
    @GetMapping("/getConfigManagerList")
    public R getConfigManagerList() {
        ArrayList<HashMap<String, String>> result = personService.getConfigManagerList();
        return R.ok().data("result", result);
    }

    @ApiOperation("根据id获取人员爬虫配置信息")
    @GetMapping("/getConfigById/{id}")
    public R getConfigById(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        PersonConfigListVO result = personService.getConfigById(id);
        return R.ok().data("result", result);
    }

    /**
     * 根据id修改配置信息
     * @param personConfigQuery
     * @return
     */
    @ApiOperation("根据id修改配置信息")
    @PostMapping("/updateById")
    public R updateById(
            @ApiParam(value = "配置信息对象", required = true) @Valid @RequestBody PersonConfigQuery personConfigQuery
    ) {
        Boolean flag = personService.updateById(personConfigQuery);
        if (!flag) {
            return R.error().code(-213).message("修改失败");
        }
        return R.ok().code(200).message("修改成功");
    }

    /**
     * 新增配置信息
     * @param personConfigQuery
     * @return
     */
    @ApiOperation("新增配置信息")
    @PostMapping("/addData")
    public R addData(
            @ApiParam(value = "配置信息对象", required = true) @Valid @RequestBody PersonConfigQuery personConfigQuery
    ) {
        Boolean flag = personService.addData(personConfigQuery);
        if (!flag) {
            return R.error().code(-213).message("新增失败");
        }
        return R.ok().code(200).message("新增成功");
    }


    @ApiOperation("根据id删除配置信息")
    @GetMapping("/foreverRemove")
    public R foreverRemove(
            @ApiParam(value = "id", required = true) @RequestParam Integer id,
            @ApiParam(value = "showId", required = true) @RequestParam Integer showId
    ) {
        personService.foreverRemove(id, showId);
        return R.ok();
    }

    @ApiOperation("绑定定时任务")
    @GetMapping("/updateScheduledName")
    public R updateScheduledName(
            @ApiParam(value = "定时任务名称", required = true) @RequestParam String scheduledName
    ) {
        Integer scheduledId = personService.updateScheduledName(scheduledName);
        return R.ok().data("scheduledId", scheduledId);
    }

    @ApiOperation("修改备注")
    @GetMapping("/updateRemarks")
    public R updateRemarks(
            @ApiParam(value = "爬虫id", required = true) @RequestParam("id") Integer id,
            @ApiParam(value = "备注", required = true) @RequestParam("remarks") String remarks
    ) {
        personService.updateRemarks(id, remarks);
        return R.ok();
    }


}
